package com.bwie.service.impl;

import com.bwie.mapper.TbActiveMapper;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.TbActive;
import com.bwie.redpacket.*;
import com.bwie.service.RedPacketService;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2025/7/28 15:20
 */

@Service
public class RedPacketServiceImpl implements RedPacketService {

    IRedPacketStrategy iRedPacketStrategy;

    @Autowired
    TbActiveMapper tbActiveMapper;

    @Autowired
    HttpServletRequest request;
    @Override
    public R getRedPacket(Integer activeId) {

        //--1 获取当前用户
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)) {
            return R.ERROR(401, "请登录");
        }

        Integer userId = TokenUtil.getUserId(token);

        //--2 根据活动的ID，获取活动信息
        TbActive tbActive = tbActiveMapper.selectById(activeId);
        if(tbActive == null) {
            return R.ERROR(404, "活动不存在");
        }


        //-- 3 抢红包
        if(tbActive.getActiveStatus().equals(1)) {
            // 单次抽奖策略
            iRedPacketStrategy = new SingleRedPacket();
        }
        else if(tbActive.getActiveStatus().equals(2)) {
            // 多次抽奖策略
            iRedPacketStrategy = new MultiTimeRedPacket();
        }
        else if(tbActive.getActiveStatus().equals(3)) {
            // 周多抽奖策略
            iRedPacketStrategy = new WeekMultiRedPacket();
        }
        else if(tbActive.getActiveStatus().equals(4)) {
            // 月多抽奖策略
            iRedPacketStrategy = new MonthMultiRedPacket();
        }
        else if(tbActive.getActiveStatus().equals(4)) {
            // 月多抽奖策略
            DayMultiRedPacket dayMultiRedPacket = new DayMultiRedPacket();
            dayMultiRedPacket.setInfo(userId, activeId);
            iRedPacketStrategy = dayMultiRedPacket;
        }
        else {
            return R.ERROR(500, "没有抽奖策略");
        }

        //--4 开始抽奖
        R r = iRedPacketStrategy.getRedPacket();

        return r;
    }
}
